package com.china08.yxyapi.api.manage.tec.teaching.scoreport.selectexam;

import java.text.ParseException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.scoreport.Exam.ThanksUser;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.service.mg.impl.ExamServiceImpl;
import com.china08.yxyapi.util.DateUtils;

/**
 * 考试成绩列表查询（多条件）
 * 
 */
@CrossOrigin
@RestController
public class SelectExamController extends YxyApiBaseController {

    @Autowired
    private ExamServiceImpl examServiceImpl;

    @RequestMapping(value = "/api_yw/manage/tec/teaching/scoreport/selectexam/pageshow", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Page<RespModel> process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody ReqModel model) throws ParseException {

        String userId = getProfile(request, response).getId();
        String schoolId = getProfile(request, response).getSchoolId();

        Pageable pageable = model.getPageable(Direction.DESC, "examDate", "createdDate");

        // 判断传输时间是否存在
        Integer startTime = -1, endTime = -1;
        if (StringUtils.isNotBlank(model.dateTime)) {
            startTime = DateUtils
                    .formatDate4(DateUtils.formatStrDate3(model.dateTime.split(" - ")[0].replace("-", "")));
            endTime = DateUtils.formatDate4(DateUtils.formatStrDate3(model.dateTime.split(" - ")[1].replace("-", "")));
        }
        Page<RespModel> page = examServiceImpl.getExamPage(userId, schoolId, model.name, model.status, model.classId,
                startTime, endTime, pageable);
        return page;
    }

    /**
     * 请求参数
     * 
     */
    protected static class ReqModel extends YxyApiPageReqModel {
        // 考试名称
        public String name;
        // 发送状态 -1为全部
        public Integer status;
        // 班级id
        public String classId;
        // 范围时间
        public String dateTime;
    }

    /**
     * 响应参数
     * 
     */
    public static class RespModel extends YxyApiRespModel {
        // 考试id
        public String id;
        // 姓名
        public String name;
        // 考试类型/科目
        public String type;
        // 班级id
        public String classId;
        // 班级名称
        public String className;
        // 考试时间
        public String examDate;
        // 状态 0--未录入数据 , 1--未发送,2--已发送
        public Integer status;
        // 感谢人集合
        public List<ThanksUser> thankUsers;
    }
}
